package com.example.computer_exam_training_sys.mapper;

import com.example.computer_exam_training_sys.vo.QuestionCommentDTO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface QuestionCommentsVOMapper {

    // 查询所有评论
    @Select("SELECT qc.comment_id,\n" +
            "       qc.question_id,\n" +
            "       q.content ,\n" +
            "       qc.user_id,\n" +
            "       u.username,\n" +
            "       qc.like_count,\n" +
            "       qc.create_time,\n" +
            "       qc.status\n" +
            "FROM question_comments qc\n" +
            "LEFT JOIN questions q ON qc.question_id = q.question_id\n" +
            "LEFT JOIN users u ON qc.user_id = u.user_id\n" +
            "WHERE qc.status = 1;  -- 只查询正常评论")
    List<QuestionCommentDTO> findAllComments();

    // 根据 commentId 查询
    @Select("SELECT qc.comment_id,\n" +
            "       qc.question_id,\n" +
            "       q.content ,\n" +
            "       qc.user_id,\n" +
            "       u.username,\n" +
            "       qc.like_count,\n" +
            "       qc.create_time,\n" +
            "       qc.status\n" +
            "FROM question_comments qc\n" +
            "LEFT JOIN questions q ON qc.question_id = q.question_id\n" +
            "LEFT JOIN users u ON qc.user_id = u.user_id\n" +
            "WHERE qc.comment_id = #{commentId};\n")
    QuestionCommentDTO findByCommentId(@Param("commentId") Integer commentId);

    // 逻辑删除
    @Delete("UPDATE question_comments\n" +
            "SET status = 0\n" +
            "WHERE comment_id = #{commentId};\n")
    int deleteByCommentId(@Param("commentId") Integer commentId);


}
